<template>
    <div>
        <el-form :model="formData" :rules="Rules"
                 ref="formDom"
                 label-width="auto">
            <el-form-item label="分类 " desc="" prop="classId"
                          disabled="false">
                <classSelectOne placeholder="请选择分类" v-model="formData.classId"
                                :text.sync="formData.className"
                                clearable
                                style="width:320px;"
                                typeInfo="bpm"
                                :classApi="{
                                 getList:classApi.getList,
                                   getItem:classApi.getItem,
                                }"
                
                />
            </el-form-item>
            <el-form-item label="名称 " desc="" prop="name"
                          disabled="false">
                <el-input placeholder="" v-model="formData.name" clearable
                          style="width:320px;"
                />
            </el-form-item>
            <el-form-item label="描述 " desc="" prop="describeInfo"
                          disabled="false">
                <el-input placeholder="" v-model="formData.describeInfo" clearable
                          style="width:320px;"
                          type="textarea"
                />
            </el-form-item>
            <!--'状态 1 待审核 5使用中 10己关闭',-->
            <!-- <el-form-item label="状态 " desc="" prop="state"
                           disabled="false">
                 <el-input placeholder="" v-model="formData.state" clearable
                           style="width:220px;"
                 />
             </el-form-item>-->
        </el-form>
    </div>
</template>
<script>
  import { fillData } from '@/utils';
  
  const Rules = {
    name: [{ required: true, message: '', trigger: 'blur' }],
    classId: [{ required: true, message: '', trigger: 'blur' }],
  };
  const init_form = {
    'id': '',
    'name': null,
    'code': null,
    'state': null,
    className: '',
    'bpmStateCode': null,
    'userGroupCode': null,
    'classId': null,
    'className': null,
    'classIdPath': null,
    'describeInfo': null,
    'remarks': null,
    'tenantType': null,
    'tenantId': null,
    'delState': null,
    'addUser': null,
    'addUserId': null,
    'addTime': null,
    'editUser': null,
    'editUserId': null,
    'lastEditTime': null,
    'serverNumber': null,
  };
  import * as lodash from 'lodash';
  import * as bpnmApi from '@/apis/bpnm.api.js';
  import { alertWithPayload } from '@base/utils';
  import { validatePromise } from '../../../utils';
  import * as classApi from '@/apis/class.api.js';
  
  export default {
    data() {
      return {
        classApi,
        Rules,
        formData: lodash.cloneDeep(init_form),
      };
    },
    components: {
      classSelectOne: () => import('@/components/classSelectOne/classSelectOne.vue'),
    },
    methods: {
      /**
       * @description:
       * @params _data.id
       * @return
       */
      async initByDialog(_data) {
        if (_data && _data.id) {
          var result = await bpnmApi.getItem(_data.id);
          _data = result && result.data;
          console.log('获取id', _data);
        }
        fillData(this.formData, _data);
        //设置默认
        if (this.formData.state <= 0) {
          this.formData.state = 1;
        }
        //填充默认 className
        if (!this.formData.className) {
          var result = await classApi.getItem(this.formData.classId);
          this.formData.className = result && result.data && result.data.name || '';
        }
        
      },
      async submitByDialog() {
        var isyes = await validatePromise.call(this, 'formDom');
        if (!isyes) {
          return { success: isyes };
        }
        var func = this.formData.id ? bpnmApi.update : bpnmApi.add;
        var result = await func(this.formData);
        alertWithPayload(result);
        return result;
      },
    },
    
  };
</script>
